package com.macro.mall.portal.util;

import com.macro.mall.model.BaseDO;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.portal.bo.AdminUserDetails;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

public class AdminUtils {

    public static UmsAdmin getUser() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication authentication = ctx.getAuthentication();
        if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
            AdminUserDetails principal = (AdminUserDetails) authentication.getPrincipal();
            return principal.getUmsAdmin();
        }
        return null;
    }

    public static <T> void setCreation(T entity) {
        UmsAdmin user = getUser();
        LocalDateTime now = LocalDateTime.now();
        long id = ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(user.getId()) ? 0L : user.getId();

        Class<?> objClass = entity.getClass();
        Field[] objFields = objClass.getDeclaredFields();
        Class<?> superClass = objClass.getSuperclass();
        Field[] superFields = superClass.getDeclaredFields();
        Field[] declaredFields = ArrayUtils.addAll(objFields, superFields);
        String fieldName;

        try {
            for (Field field : declaredFields) {
                field.setAccessible(true);
                fieldName = field.getName();
                switch (fieldName) {
                    case "isDelete":
                        field.set(entity, 0);
                        break;
                    case "createUser":
                        field.set(entity, id);
                        break;
                    case "createTime":
                        field.set(entity, now);
                        break;
                    default:
                        break;
                }
            }
        } catch (IllegalAccessException e) {
        }
    }

    public static <T> void setEdition(T entity) {
        UmsAdmin user = getUser();
        LocalDateTime now = LocalDateTime.now();
        long id = ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(user.getId()) ? 0L : user.getId();

        Class<?> objClass = entity.getClass();
        Field[] objFields = objClass.getDeclaredFields();
        Class<?> superClass = objClass.getSuperclass();
        Field[] superFields = superClass.getDeclaredFields();
        Field[] declaredFields = ArrayUtils.addAll(objFields, superFields);
        String fieldName;

        try {
            for (Field field : declaredFields) {
                field.setAccessible(true);
                fieldName = field.getName();
                switch (fieldName) {
                    case "isDelete":
                        field.set(entity, 0);
                        break;
                    case "updateUser":
                        field.set(entity, id);
                        break;
                    case "updateTime":
                        field.set(entity, now);
                        break;
                    default:
                        break;
                }
            }
        } catch (IllegalAccessException e) {
        }
    }

    public static <T extends BaseDO> void setDelete(T entity) {
        UmsAdmin user = getUser();
        LocalDateTime now = LocalDateTime.now();
        long id = ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(user.getId()) ? 0L : user.getId();

        Class<?> objClass = entity.getClass();
        Field[] objFields = objClass.getDeclaredFields();
        Class<?> superClass = objClass.getSuperclass();
        Field[] superFields = superClass.getDeclaredFields();
        Field[] declaredFields = ArrayUtils.addAll(objFields, superFields);
        String fieldName;

        try {
            for (Field field : declaredFields) {
                field.setAccessible(true);
                fieldName = field.getName();
                System.out.println(fieldName);
                switch (fieldName) {
                    case "isDelete":
                        field.set(entity, 1);
                        break;
                    case "updateUser":
                        field.set(entity, id);
                        break;
                    case "updateTime":
                        field.set(entity, now);
                        break;
                    default:
                        break;
                }
            }
        } catch (IllegalAccessException e) {
        }
    }

}
